package com.camps.sdk.utils;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * @Author ldaoliang
 * @Date create in 11:30 2021/8/31
 * @Description
 */
public class EncryptionAndDecryptionUtils {

    /*
     * skey
     */
    private static final String sKey = "7srxUwITlQqoifRD"; //16字节
    /**
     * 向量
     */
    private static final String default_iv = "NwC49J6bWSMsFAfz"; //16字节


    /**
     * 带向量加密，使用CBC模式，需要一个向量iv，可增加加密算法的强度
     *
     * @param plaintext 待加密的字符串
     * @return 加密后的值
     */
    public static String encrypt(String plaintext) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES");
        IvParameterSpec iv = new IvParameterSpec(default_iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
        byte[] encrypted = cipher.doFinal(plaintext.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    /**
     * 带向量解密
     *
     * @param cipherText 密文
     * @return 解密后的值
     */
    public static String decrypt(String cipherText) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES");
        IvParameterSpec iv = new IvParameterSpec(default_iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
        byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(cipherText.getBytes()));
        return new String(decrypted);
    }

    /*
     * 转为二进制
     */
    public static byte[] hexStr2Byte(String secret_key) {
        if (secret_key.length() < 1){
            return null;
        }
        byte[] encrypted = new byte[secret_key.length() / 2];
        for (int i = 0; i < secret_key.length() / 2; i++) {
            int high = Integer.parseInt(secret_key.substring(i * 2, i * 2 + 1), 16);//偶数位
            int low = Integer.parseInt(secret_key.substring(i * 2 + 1, i * 2 + 2), 16);//奇数位
            encrypted[i] = (byte) (high * 16 + low);
        }
        return encrypted;
    }

    /*
     * 转为十六进制字符串
     */
    public static String byte2HexStr(byte buf[]) {
        StringBuffer strbuf = new StringBuffer(buf.length * 2);
        int i;
        for (i = 0; i < buf.length; i++) {
            if (((int) buf[i] & 0xff) < 0x10){
                strbuf.append("0");
            }
            strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
        }
        return strbuf.toString();
    }

    /*public static void main(String[] args) throws Exception {
        String s = "*WT,100000210111000,POWERON,170122,m1,595,MFD03_CTA_001,V,ALOE01002708,ALOE01002708,13912345678,FFFFDFFF#";
        System.out.println(encrypt(s));
    }*/

    public static void main(String[] args) {
        try {
            String decrypt = decrypt("QVFYlRji9tQ1/7GC7oJW70YUUEp1VCDlgawf1tb8WAqA0jc/FWeDTM3TMwBhIuOW+wckSWqfSfWTTCRemZTj2omGXwvb9UC2PSvjFQuFNePsKl2TOS7xpY9bFbjeHMCjNplsMJb3qPzEQH3uifeXfuMhtBHhwpRxFozJJdukOss=");
            System.out.println(decrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
